﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;

namespace 包覆门.OtherDecManager
{
    public partial class OtherMainEditing : Form
    {
        public OtherMainEditing()
        {
            InitializeComponent();
        }
        private OtherDecDAL dal = new OtherDecDAL();
        public bool IsNew { get;set; }
        public string EditingId { get; set; }
        //加载
        private void OtherMainEditing_Load(object sender, EventArgs e)
        {
            if (IsNew){} 
            else 
            {
                OtherMainDec dec = dal.GetOtherMainDecById(EditingId);
                this.tbmxbh.Text = dec.Mxbhid;
                this.tbDecname.Text = dec.DecName;
            }
        }
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            OtherMainDec dec = new OtherMainDec();
            dec.DecName = this.tbDecname.Text.Trim();
            dec.Mxbhid = this.tbmxbh.Text.Trim();
            if (!ValidateData(dec)) return;
            if (IsNew)
            {
                int result = dal.SaveMainOtherDec(dec);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
            else {
                dec.Oid = Convert.ToInt32(EditingId);
                DataTable dt = dal.CheckOtherDetailDecByParentId(EditingId);
                if (dt.Rows.Count > 0)
                {
                    if (MessageBox.Show("存在明细项，确定要修改么", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        int result = dal.UpdateMainAndDetailOtherDec(dec);
                        if (result > 1)
                        {
                            MessageBox.Show("保存成功");
                            DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            MessageBox.Show("保存失败");
                            DialogResult = DialogResult.No;
                        }
                    }

                }
                else
                {
                    int result = dal.UpdateMainOtherDec(dec);
                    if (result == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }
                }
            }

        }
        //验证数据
        private bool ValidateData(OtherMainDec dec) 
        {
            bool flag = true;
            if (string.IsNullOrWhiteSpace(dec.Mxbhid)) {
                MessageBox.Show("门型编号不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(dec.DecName)) {
                MessageBox.Show("另加类名称不能为空");
                return false;
            }
            return flag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }
        
    }
}
